
import net from "net"
import SmbRequest from "../tool/SmbRequest.js";
import SocketStatus from "../enum/SocketStatus.js";
export default class Connect {

  processId = 0
  sessionId = 0
  messageId = 0
  treeId = 0
  
  isConnect = false
  onResponse = ()=>{}
  onConnect = ()=>{}
  onError = ()=>{}
  responseBuffer = Buffer.allocUnsafe(0)
  agreeObj

  constructor(opt){

    this.ip = opt.host
    this.port = opt.port
    this.share = "share"

    this.userName = opt.userName
    this.password = opt.password
    this.domain = opt.domain

    this.socket = new net.Socket({
      allowHalfOpen: true,
    });


    this.socket.on('data', (response) => {
      // 结构数据
      SmbRequest.onResponse(SocketStatus.DATA, this, response)
      
    });

    this.socket.on('end', (response) => {
      console.log('连接关闭')
      SmbRequest.onResponse(SocketStatus.END, this, response)
    });

    this.socket.on('connect', (response) => {
      console.log("连接目标机器成功")
      this.isConnect = true
      this.onConnect()
      this.onConnect = ()=>{}
    });

    this.socket.on('error', (err)=> {
      console.log('--socket error--'); 
      this.onError(err)
      this.onError = ()=>{}
    });
      

  }

  async initConnect(cb){
    return new Promise((resolve, reject) => {
      if (this.isConnect) {
        resolve()
        return
      }
      this.socket.connect(this.port, this.ip);
      this.onConnect = resolve
      this.onError = reject

    })
  }


}